package com.runmi.webchat.aftersale.web.set.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.runmi.webchat.aftersale.common.page.PageModel;
import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.common.utils.MD5;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.dbcore.entity.Role;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.mapper.RoleMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.UserMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseService;

import com.runmi.webchat.aftersale.web.set.model.UserEdit;
import com.runmi.webchat.aftersale.web.set.model.UserIndex;
import com.runmi.webchat.aftersale.web.set.model.UserSaveMerchant;

@Service
public class UserService extends BaseService{
	
	@Autowired
	private  RoleMapper roleMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	public void index(
			UserIndex model){		
		 
			try{		
				
				List<Role> roles= roleMapper.getAllRoleInfo();
				model.setRoles(roles);
				
			    PageHelper.startPage(model.getCpage(), model.getPageSize());
				List<Map> items=userMapper.getScmUserByName(
						model.getUserName(),
						model.getRealName(), 
						model.getRoleId());
				PageInfo<Map> pageInfo = new PageInfo<Map>(items);
				model.setTotalCount((int)pageInfo.getTotal());
				model.setItems(items);
			}catch(Exception ex){
				ex.printStackTrace();
				
			}

	}
	
	public void edit(UserEdit model){
		
		List<Role> roles= roleMapper.getAllRoleInfo();
		model.setRoles(roles);
		
		User user= userMapper.selectByPrimaryKey(model.getUserId());
		user=user==null?new User():user;
		model.setUser(user);
		
	}
	
	public Result getUserById(Integer userId){
		Result result=null;
		try{
			User scmUser= userMapper.selectByPrimaryKey(userId);
			if(null!=scmUser){
				result=ResultFactory.getResultSuccess(scmUser);
			}else{
				result=ResultFactory.getResultFail("未查询到用户信息");
			}
		}catch(Exception ex){
			ex.printStackTrace();
			result=ResultFactory.getResultFail();
		}
		return result;
	}
	
//	public void edit(UserEdit model){
//		
//		List<ScmRole> roles= scmRoleMapper.getAllScmRoleInfo();
//		model.setRoles(roles);
//		
//		List<ScmJobInfo> jobInfos= scmJobInfoMapper.getAllJobInfo();
//		model.setJobInfos(jobInfos);
//			
//		ScmUser user= scmUserMapper.selectByPrimaryKey(model.getUserId());
//		user=user==null?new ScmUser():user;
//		model.setUser(user);
//		
//	}
	public Map<String,String> saveUser(UserEdit model){
		Map<String,String> result=new HashMap<String,String>();
		if(null!=model.getUserId()){
			User erpUser= userMapper.selectByPrimaryKey(model.getUserId());
			if(null!=erpUser){
				if(erpUser.getUserName().equals("administrator")){
					result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
					result.put(Constants.RETURN_MESSAGE_KEY, "管理员用户不能修改");		
					return result;
				}
				if(!erpUser.getUserName().equals(model.getUserName())){
					User erpUser1=new User();
					erpUser1.setUserName(model.getUserName());
					erpUser1.setDelFlag((byte)0);
					int num= userMapper.selectCount(erpUser1);
					if(num>0){
						result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
						result.put(Constants.RETURN_MESSAGE_KEY, "用户名重复");		
						return result;
					}
				}
//				if(StringUtil.isNotEmptyStr(model.getMobile())){
//					if(!StringUtil.isMobileNum(model.getMobile())){
//						result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//						result.put(Constants.RETURN_MESSAGE_KEY, "手机号码有误");		
//						return result;
//					}
//				}
				
				if(StringUtil.isNotEmptyStr(model.getEmail())){
					if(!StringUtil.checkEmail(model.getEmail())){
						result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
						result.put(Constants.RETURN_MESSAGE_KEY, "邮箱有误");		
						return result;
					}
				}
				
				User record=new User();
				record.setId(erpUser.getId());
				record.setRealName(model.getRealName());
				record.setRoleId(model.getRoleId());
				record.setUserName(model.getUserName());
				record.setMobile(model.getMobile());
				record.setEmail(model.getEmail());
				record.setUpdateTime(new Date());
				record.setJobId(model.getJobId());
				record.setJobName(model.getJobName());
				userMapper.updateByPrimaryKeySelective(record);
			}else{
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "未查询到用户");					
				return result;
			}
		}else{
			
			User wmsAdmin1=new User();
			wmsAdmin1.setUserName(model.getUserName());
			wmsAdmin1.setDelFlag((byte)0);
			int num= userMapper.selectCount(wmsAdmin1);
			if(num>0){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "用户名重复");		
				return result;
			}
			
			User record=new User();
			record.setRealName(model.getRealName());
			record.setUserName(model.getUserName());
			record.setScmRoleId(model.getRoleId());
			record.setMobile(model.getMobile());
			record.setCreateTime(new Date());
			record.setUpdateTime(new Date());
			record.setStatus((byte)1);
			record.setDelFlag((byte)0);
			record.setJobId(model.getJobId());
			record.setEmail(model.getEmail());
			record.setJobName(model.getJobName());
			record.setType((byte)1);
			String password="111111";
			String psdmd5=MD5.getMD5(password);
			record.setPassword(psdmd5);
			userMapper.insertSelective(record);
		}
		result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
		result.put(Constants.RETURN_MESSAGE_KEY, "success");	
		return result;
	}
	
	
	
	public Map<String,String> changeStatus(User erpUser, Integer userId,Byte opType){
		Map<String,String> result=new HashMap<String,String>();
		try{
			this.log.info("用户" + erpUser.getRealName() + " 操作:" + userId + " 操作类型:" + opType );
			if(userId==null || null==opType){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");					
				return result;
			}
			User erpUserMerchant= userMapper.selectByPrimaryKey(userId);
			if(null==erpUserMerchant){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "未查询到用户");					
				return result;
			}
			if(opType.intValue()==1){
				User record=new User();
				record.setId(userId);
				record.setUpdateTime(new Date());
				record.setStatus((byte)2);							
				userMapper.updateByPrimaryKeySelective(record);
			}
			if(opType.intValue()==2){
				//冻结
				User record=new User();
				record.setId(userId);
				record.setUpdateTime(new Date());
				record.setStatus((byte)1);							
				userMapper.updateByPrimaryKeySelective(record);
			}	
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
			result.put(Constants.RETURN_MESSAGE_KEY, "success");		
			
			
		}catch(Exception ex){
			ex.printStackTrace();
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
		}
		return result;
	}
	public Result del(User user, Integer userId){
		Result result= ResultFactory.getResultFail();
		try{
			this.log.info("用户" + user.getRealName() + "  del user:" + userId );
			if(userId==null){
//				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");		
				result=ResultFactory.getResultFail("数据异常");
				return result;
			}
			User model= userMapper.selectByPrimaryKey(userId);
			if(null==model){	
				result=ResultFactory.getResultFail("未查询到用户");
				return result;
			}
			
			User record=new User();
			record.setId(model.getId());
			record.setUpdateTime(new Date());
			record.setDelFlag((byte)1);		
			
			userMapper.updateByPrimaryKeySelective(record);
			
			result=	ResultFactory.getResultSuccess();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
			result=ResultFactory.getResultFail(ex.getMessage());
//			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//			result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
		}
		return result;
	}
	
	public Map<String,String> resetPassword(Integer userId){
		Map<String,String> result=new HashMap<String,String>();
		try{
			if(userId==null){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");					
				return result;
			}
			User erpUser= userMapper.selectByPrimaryKey(userId);
			if(null==erpUser){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "未查询到用户");					
				return result;
			}
			String password="111111";
			String psdmd5=MD5.getMD5(password);
			User record=new User();
			record.setId(userId);
			record.setPassword(psdmd5);
			record.setUpdateTime(new Date());
			userMapper.updateByPrimaryKeySelective(record);
			
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
			result.put(Constants.RETURN_MESSAGE_KEY, "success");		
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
}
